package com.bt.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bt.mapper.DtsBrandMapper;
import com.bt.pojo.DtsBrand;
import com.bt.service.DtsBrandService;
import com.bt.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/23 16:37
 **/
@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Override
    public List<DtsBrandVo> findDtsBrandVos() {
        List<DtsBrand> dtsBrands = dtsBrandMapper.selectList(new QueryWrapper<DtsBrand>().select("id", "name"));
        return dtsBrands.stream().map(dtsBrand -> {
            DtsBrandVo dtsBrandVo = new DtsBrandVo();
            dtsBrandVo.setValue(dtsBrand.getId());
            dtsBrandVo.setLabel(dtsBrand.getName());
            return dtsBrandVo;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<DtsBrand> findBrandByPage(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        return dtsBrandMapper.selectPage(new Page<DtsBrand>(page,limit),new QueryWrapper<DtsBrand>().eq(id!=null,"id",id).like(name!=null,"name",name).orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort));
    }

    @Override
    public void updateDtsBrand(DtsBrand dtsBrand) {
       dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void insertDtsBrand(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void deleteDtsBrand(DtsBrand dtsBrand) {
        dtsBrandMapper.deleteById(dtsBrand);
    }
}
